#include "prefix.h"
#include "gui_al.h"
#include "callbacks.h"
#include "error.h"
#include "settings.h"
#include "interface.h"
#include "gui_io.h"
#include "threads.h"






int _lock_status = 0;

int _lock_mode = 0;

void elog_gui_al_lock()
{
	 elog_glob_lock();
}

void elog_gui_al_unlock()
{
	 elog_glob_unlock();
}
void elog_gui_al_setLockMode(int mode)
{
	_lock_mode = mode;
}



int opened = 0;


int elog_gui_al_attached = 0;	//whether there's something attached to view menu


void elog_gui_al_win_title(const char *title)
{
	gtk_window_set_title(GTK_WINDOW(_mainWindow), (gchar *) title);
}

int elog_gui_al_page(int page)
{

	if (page < 0) {
		int ret =
		    gtk_notebook_get_current_page(GTK_NOTEBOOK(_notebook));

		return ret;
	}
	gtk_notebook_set_current_page(GTK_NOTEBOOK(_notebook), page);

	return -1;
}

int elog_gui_al_width()
{				//checking the actual width can cause some problems at startup.
	int width = elog_set_get_int("width");
	return width;
}

int elog_gui_al_pane(int pos)
{

	if (pos == -1) {
		int ret = gtk_paned_get_position(GTK_PANED(_mainHPane));
		return ret;
	}
	gtk_paned_set_position(GTK_PANED(_mainHPane), pos);


	return -1;

}
void elog_gui_al_srch_pane(int pos)
{
	gtk_paned_set_position(GTK_PANED(_res_srch_pane), pos);
}



struct elog_xml_link **elog_gui_al_getLinks()
{
	struct elog_xml_link **links;
	//const gchar *txt = gtk_label_get_text(GTK_LABEL(_links));
	links = NULL;		//temp line!  FEATURE DISABLED!
	return links;
}



gboolean on_lnk_event(GtkTextTag * texttag,
		      GObject * arg1,
		      GdkEvent * event,
		      GtkTextIter * arg2, gpointer user_data)
{
	printf("event for link\n");
	return FALSE;
}


void elog_gui_al_clearDay()
{

	gtk_list_store_clear(GTK_LIST_STORE(_mainDayList));

}
void elog_gui_al_addItemToDay(const char *title, const char *path,
			      int mood)
{
	GError *error = NULL;
	GdkPixbufLoader *loader = gdk_pixbuf_loader_new();
	GdkPixbuf *scaled = NULL;


	if (mood < -1)
		return;

	int fr = 0;
	void *pix = NULL;
	unsigned int size;
	if (mood >= 0) {
		char *path;
		elog_sp_cat(&path, DATADIR, "/ejourn/img/ejourn.png",
			    NULL);
		struct elog_io_file *f =
		    elog_io_initialize(path, ELOG_IO_READ);
		if (f) {
			pix = elog_io_readRestofFile(f, &size);
			elog_io_close(f);
			fr = 1;
		} else {
			elog_err_print("Error, missing page_32.png\n");
			return;
		}
		free(path);

	} else {
		char *path;
		elog_sp_cat(&path, DATADIR, "/ejourn/img/add2.png", NULL);
		struct elog_io_file *f =
		    elog_io_initialize(path, ELOG_IO_READ);
		if (f) {
			pix = elog_io_readRestofFile(f, &size);
			elog_io_close(f);
			fr = 1;
		} else {
			elog_err_print("Error, missing add.png\n");
		}
		free(path);
	}


	gdk_pixbuf_loader_write(loader, pix, size, &error);
	GdkPixbuf *mood_pb = gdk_pixbuf_loader_get_pixbuf(loader);
	gdk_pixbuf_loader_close(loader, &error);

	if (pix)
		free(pix);

	scaled = mood_pb;




	GtkTreeIter iter;
	gtk_list_store_append(GTK_LIST_STORE(_mainDayList), &iter);
	gtk_list_store_set(GTK_LIST_STORE(_mainDayList), &iter, 0, title,
			   1, path, 2, scaled, -1);
	g_object_unref(loader);
}
int elog_gui_al_setKey(int mode)
{
	setKey(NULL);
	//      create_intro_window();  
	return create_login_window(mode);
}


char *elog_gui_al_clipBoard(char *text)
{
	if (text) {
		gtk_clipboard_set_text(gtk_clipboard_get_for_display
				       (gdk_display_get_default(),
					GDK_SELECTION_CLIPBOARD), text,
				       strlen(text));
		return NULL;
	}
	return (char *)
	    gtk_clipboard_wait_for_text(gtk_clipboard_get_for_display
					(gdk_display_get_default(),
					 GDK_SELECTION_CLIPBOARD));
}


/*****************************************************/





/****************Calendar Functions*******************/

struct cal_date *elog_gui_al_calendarDate(struct cal_date *date)
{

	if (date == NULL) {
		struct cal_date *ret = malloc(sizeof *ret);
		gtk_calendar_get_date(GTK_CALENDAR(_calendar),
				      &(ret->year), &(ret->month),
				      &(ret->day));

		return ret;
	}
	gtk_calendar_freeze(GTK_CALENDAR(_calendar));
	gtk_calendar_select_month(GTK_CALENDAR(_calendar), date->month,
				  date->year);
	gtk_calendar_select_day(GTK_CALENDAR(_calendar), date->day);
	gtk_calendar_thaw(GTK_CALENDAR(_calendar));

	return NULL;
}
void elog_gui_al_markDate(int day)
{

	gtk_calendar_mark_day(GTK_CALENDAR(_calendar), day);

}

void elog_gui_al_clearMarks()
{

	gtk_calendar_clear_marks(GTK_CALENDAR(_calendar));

}

/************************************/


/***********link stuff*/
char *elog_gui_al_getTreeSelected()
{

	gchar *s;
	gchar *d;
	GtkTreeSelection *selection;
	GtkTreeModel *model =
	    gtk_tree_view_get_model(GTK_TREE_VIEW(_treeViewLink));
	GtkTreeIter iter;

	/* This will only work in single or browse selection mode! */

	selection =
	    gtk_tree_view_get_selection(GTK_TREE_VIEW(_treeViewLink));
	if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
		gtk_tree_model_get(model, &iter, 0, &s, 1, &d, -1);
		char *ret;
		elog_sp_cat(&ret, d, NULL);

		return ret;
	}

	return NULL;
}

void elog_gui_al_msg_win_pulse(struct elog_gui_al_msg_win *win)
{
	gtk_progress_bar_pulse(GTK_PROGRESS_BAR(win->progress));
	gtk_progress_bar_set_text(GTK_PROGRESS_BAR(win->progress),
				  "Please wait..");

	gtk_widget_grab_focus(win->label);

	gtk_widget_queue_draw(win->win);
	gtk_widget_queue_draw(win->label);
	gtk_widget_queue_draw(win->progress);

	//now clear all events.
	while (gtk_events_pending())
		gtk_main_iteration();


}

struct elog_gui_al_msg_win *elog_gui_al_msg_win_create(const char *msg)
{
	struct elog_gui_al_msg_win *nue = malloc(sizeof *nue);
	nue->win = gtk_dialog_new();
	gtk_window_set_decorated(GTK_WINDOW(nue->win), FALSE);

	char *wmsg;
	elog_sp_cat(&wmsg, "Caution:\n", msg, NULL);
	GtkWidget *label = gtk_label_new(wmsg);
	free(wmsg);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(nue->win)->vbox), label,
			   TRUE, TRUE, 0);

	nue->progress = gtk_progress_bar_new();
	gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(nue->progress),
					0.03);
	//gtk_progress_bar_set_bar_style(GTK_PROGRESS_BAR(nue->progress), GTK_PROGRESS_DISCRETE);
	gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(nue->progress),
					 GTK_PROGRESS_LEFT_TO_RIGHT);
	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(nue->win)->vbox),
			   nue->progress, TRUE, TRUE, 0);
	gtk_widget_show_all(nue->win);


	//now clear all events.
	while (gtk_events_pending())
		gtk_main_iteration_do(FALSE);

	nue->label = label;
	nue->n = 0;
	elog_sp_cat(&(nue->txt), msg, NULL);

	return nue;
}

void elog_gui_al_msg_win_destroy(struct elog_gui_al_msg_win *msg)
{
	gtk_widget_destroy(msg->win);
	free(msg);
}

/*****Progress bar:**********/

void elog_gui_al_progress_pulse()
{
	gtk_progress_bar_pulse(GTK_PROGRESS_BAR(_progress));

}

void elog_gui_al_progress_set(float to)
{
	gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(_progress),
				      (double) to);

}


unsigned int elog_gui_al_push_status(char *status)
{
	guint context =
	    gtk_statusbar_get_context_id(GTK_STATUSBAR(_status), status);
	gtk_statusbar_push(GTK_STATUSBAR(_status), context, status);

	return (unsigned int) context;
}

void elog_gui_al_pop_status(unsigned int id)
{
	gtk_statusbar_pop(GTK_STATUSBAR(_status), id);

}


/*****View menu stuff****/
GtkWidget *_attached_to_view = NULL;

void on_view_all_activate(GtkMenuItem * menuitem, gpointer user_data)
{
	void (*callback) () = user_data;
	callback();
}
struct elog_gui_al_menu *elog_gui_al_menu_new()
{
	struct elog_gui_al_menu *ret =
	    malloc(sizeof(struct elog_gui_al_menu));
	ret->parent = NULL;
	ret->widget = gtk_menu_new();
	int c;
	for (c = 0; c < 16; ++c)
		ret->groups[c] = NULL;
	ret->num = 0;
	return ret;
}
void elog_gui_al_menu_attach_view(struct elog_gui_al_menu *menu)
{
	_attached_to_view = menu->widget;
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(_viewMenu),
				  (GtkWidget *) (menu->widget));
	menu->parent = _viewMenu;
	g_object_ref(menu->widget);	//otherwise it disappears.
}
void elog_gui_al_menu_detach(struct elog_gui_al_menu *menu)
{
	g_object_ref(menu->widget);	//otherwise it disappears.
	menu->parent = NULL;
	gtk_menu_detach(GTK_MENU(menu->parent));
}

void elog_gui_al_menu_detach_view()
{
	if (_attached_to_view) {
		g_object_ref(_attached_to_view);
		gtk_menu_detach(GTK_MENU(_attached_to_view));
		_attached_to_view = NULL;
	}
}

void elog_gui_al_menu_free(struct elog_gui_al_menu *menu)
{
	gtk_widget_destroy(menu->widget);
	free(menu);
}


int elog_gui_al_view_add(struct elog_gui_al_menu *menu, const char *title,
			 void (*callback) (), int group)
{
	GtkWidget *item = NULL;
	if (group == -1)
		item = gtk_menu_item_new_with_label((const gchar *) title);
	else if (group == 0)
		item =
		    gtk_check_menu_item_new_with_label((const gchar *)
						       title);
	else if (group > 0) {
		if (!(menu->groups[group])) {
			item =
			    gtk_radio_menu_item_new_with_label(NULL,
							       title);
			menu->groups[group] = item;
		} else
			item =
			    gtk_radio_menu_item_new_with_label_from_widget
			    (menu->groups[group], title);

	}
	gtk_widget_show(item);
	g_signal_connect((gpointer) item, "activate",
			 G_CALLBACK(on_view_all_activate), callback);
	gtk_container_add(GTK_CONTAINER(menu->widget), item);

	return ++(menu->num);
}

/*****************************************************/

int elog_gui_al_locToOffset(int loc, const char *str)
{
	return g_utf8_pointer_to_offset(str, &(str[loc]));
}
int elog_gui_al_strlen(const char *str)
{
	return (int) g_utf8_strlen(str, -1);
}
int elog_gui_al_n_tabs()
{
	return gtk_notebook_get_n_pages(GTK_NOTEBOOK(_notebook));
}
